package com.ltu.service.impl;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltu.base.State;
import com.ltu.domain.mp_entity.AccountEntity;
import com.ltu.domain.mp_entity.Role;
import com.ltu.mapper.AccountMapper;
import com.ltu.model.request.UpdatePasswordReq;
import com.ltu.model.request.account.AccountPageReq;
import com.ltu.model.request.account.AccountReq;
import com.ltu.model.request.base.BaseIdReq;
import com.ltu.model.response.CodeResp;
import com.ltu.model.response.base.CodeDataResp;
import com.ltu.service.AccountService;
import com.ltu.service.RoleService;
import com.ltu.util.BeanMapper;
import com.ltu.util.common.MpPageUtil;
import com.ltu.util.common.StrUtils;
import com.ltu.util.exception.ApiException;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Vescky.L
 * @since 2019-10-22
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountEntity> implements AccountService {

    private final RoleService roleService;

    @Override
    public void saveAccount(AccountReq accountReq) {
        Role role=roleService.getRoleById(accountReq.getRoleId());
        if (role==null)
            throw new ApiException("不存在此角色，角色id有误");

        if (StrUtils.isTrimNull(accountReq.getId())){
            //新增
            AccountEntity a = new AccountEntity();
            BeanMapper.copy(accountReq,a);
            if (!StrUtils.isTrimNull(accountReq.getPassword()))
                a.setPassword(accountReq.getPassword());
            a.setRoleName(role.getRoleName());
            super.save(a);
        }
        else {
            AccountEntity a = super.getById(accountReq.getId());
            BeanMapper.copy(accountReq,a);
            super.updateById(a);
        }
    }


    @Override
    public CodeResp updatePassword(UpdatePasswordReq updatePasswordReq) {
        AccountEntity accountEntity = super.getById(updatePasswordReq.getId());
        if (null == accountEntity) {
            return  CodeResp.codeRespInit(State.CODE_FAILED, "账号不存在");
        }

        if (!updatePasswordReq.getOldPassword().equals(accountEntity.getPassword())) {
            return  CodeResp.codeRespInit(State.CODE_FAILED, "原密码不正确");
        }
        if (!StrUtils.isTrimNull(updatePasswordReq.getPassword())) {
            accountEntity.setPassword(updatePasswordReq.getPassword());
        }
        super.updateById(accountEntity);
        return CodeResp.valueOfSuccess();
    }


    @Override
    public CodeDataResp accountlist(AccountPageReq pageReq) {
        Page<AccountEntity> page = MpPageUtil.getCommonPage(pageReq.getPageReq());
        QueryWrapper<AccountEntity> condition = new QueryWrapper<>();

        //根据手机号筛选
        if (!StrUtils.isTrimNull(pageReq.getMobile())) {
            condition.like("mobile",pageReq.getMobile());
        }

        //根据登录账号筛选
        if (!StrUtils.isTrimNull(pageReq.getUserName())) {
            condition.eq("user_name",pageReq.getUserName());
        }

        //根据角色筛选
        if (!StrUtils.isTrimNull(pageReq.getRoleId())) {
            condition.like("role_id",pageReq.getRoleId());
        }

        //根据时间筛选
        //start
        if (!StrUtils.isTrimNull(pageReq.getStartTime())) {
            condition.ge("create_time",pageReq.getStartTime());
        }
        //end
        if (!StrUtils.isTrimNull(pageReq.getEndTime())) {
            condition.le("create_time",pageReq.getEndTime());
        }

        return CodeDataResp.valueOfSuccess( this.page(page, condition));
    }

  


    @Override
    public CodeResp deleteAcctount(BaseIdReq baseIdReq) {
        AccountEntity accountEntity = this.getById(baseIdReq.getId());
        if (accountEntity==null)
            return new CodeDataResp(State.CODE_FAILED,"删除账号不存在");
        super.removeById(accountEntity.getId());
        return CodeDataResp.valueOfSuccess();
    }


	@Override
	public List<AccountEntity> getAccount3() {
		// TODO Auto-generated method stub
		QueryWrapper<AccountEntity> qw = new QueryWrapper<AccountEntity>();
		qw.lambda().eq(AccountEntity :: getRoleId, 3);
		List<AccountEntity> datas = this.list(qw);
		return datas;
	}


	



 



}
